package mobile

import (

	// "sword"

	"fmt"
	"os"
	"strings"
	"tcsc/models"
	"tcsc/sword"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/toolbox"
)

func init() {
	tkorderconfirm := toolbox.NewTask("orderconfirm", "0 */10 * * * *", func() error {
		now := time.Now().Local()
		now = now.AddDate(0, 0, -7)
		nows := now.Format(Tparse)
		order := models.Query(`select a.id from mt_order a where a.status=3 and a.shippingtime>'` + nows + `'`)
		for _, v := range order {
			if err := OrderConfirmF(v["id"].(string)); err != nil {
				return err
			}
		}
		return nil
	})
	err := tkorderconfirm.Run()
	if err != nil {
		beego.Debug(err)
	} else {
		toolbox.AddTask("orderconfirm", tkorderconfirm)
	}
	tkorderevaluation := toolbox.NewTask("orderevaluation", "0 */10 * * * *", func() error {
		now := time.Now().Local()
		nowsi := now.Format(Tparse)
		now = now.AddDate(0, 0, -7)
		nows := now.Format(Tparse)
		order := models.Query(`select id,uid,carid from mt_order where status=4 and goodtime>'` + nows + `'`)
		for _, v := range order {
			if err := models.Query(`update mt_order set status=5 where id=` + v["id"].(string)); err != nil {
				beego.Debug(err)
			}
			uid := fmt.Sprint(v["uid"])
			carid := strings.Split(fmt.Sprint(v["carid"]), ",")
			for _, cid := range carid {
				if err := models.Exec(`insert into mt_product_evaluation (uid,cid,score,addtime) values (` + uid + `,` + cid + `,5,'` + nowsi + `')`); err != nil {
					beego.Debug(err)
				}
			}
		}
		return nil
	})
	err = tkorderevaluation.Run()
	if err != nil {
		beego.Debug(err)
	} else {
		toolbox.AddTask("orderevaluation", tkorderevaluation)
	}
	tkvipcancle := toolbox.NewTask("vipcancle", "0 */10 * * * *", func() error {
		now := time.Now().Local()
		now = now.AddDate(0, 0, -15)
		nows := now.Format(Tparse)
		zshy := models.Query(`select id from mt_member where vip=1 and status=1`)
		for _, v := range zshy {
			order := models.Query(`select id from mt_order where uid=` + v["id"].(string) + ` and status>1 and paytime>'` + nows + `'`)
			if order == nil {
				if err := models.Exec(`update mt_member set vip=0 where id=` + v["id"].(string)); err != nil {
					beego.Debug(err)
					return err
				}
			}
		}
		return nil
	})
	err = tkvipcancle.Run()
	if err != nil {
		beego.Debug(err)
	} else {
		toolbox.AddTask("vipcancle", tkvipcancle)
	}

	tksalse := toolbox.NewTask("tksalse", "0 */10 * * * *", func() error {
		now := sword.Gettimes("")
		zshy := models.Query(`select id,pid from mt_active_sales where end=0 and status=1 and endtime<?`, now)
		for _, v := range zshy {
			if err := models.Exec(`update mt_active_sales set end=1 where id=?`, v["id"]); err != nil {
				beego.Debug(err)
			}
			go FixPrice(fmt.Sprint(v["pid"]))
		}
		return nil
	})
	err = tksalse.Run()
	if err != nil {
		beego.Debug(err)
	} else {
		toolbox.AddTask("tksalse", tksalse)
	}
	toolbox.StartTask()

	// defer toolbox.StopTask()
}

//	func ocdPush(oids string) {
//		beego.Debug("11111")
//		nowtimes := sword.Gettimes("")
//		order := models.Query(`select a.gid,a.ordernum,a.money,a.addtime,a.paytype,a.carid,b.wx_openid_gzh,b.monicker,b.mobile from mt_order a
//			left join mt_member b on a.uid=b.id where a.id in(` + oids + `)`)
//		for _, v := range order {
//			gid := fmt.Sprint(v["gid"])
//			if gid == "0" {
//				carid := fmt.Sprint(v["carid"])
//				shop := models.Query(`select c.id,c.bid from mt_order_car a left join mt_product b on a.pid=b.id left join mt_shop c on c.bid=b.bid where a.id in(` + carid + `) group by b.bid`)
//				for _, vt := range shop {
//					bids := fmt.Sprint(vt["bid"])
//					s := models.Query(`select a.name,b.wx_openid_gzh from mt_shop a left join mt_member b on a.uid=b.id left join mt_brand c on c.id=a.bid where c.id=` + bids)[0]
//					sopenid := fmt.Sprint(s["wx_openid_gzh"])
//					beego.Debug(s)
//					beego.Debug(sopenid)
//					pr := models.Query(`select b.title,a.num from mt_order_car a join (select * from mt_product where bid=` + bids + `) b
//					on a.pid=b.id where a.id in(` + carid + `)`)
//					var pname []string
//					for _, p := range pr {
//						pname = append(pname, fmt.Sprintf(`%v X %v`, p["title"], p["num"]))
//					}
//					// driver := models.Query(`
//					// 	select wx_openid_gzh,concat(',',driverarea,',') as driverarea
//					// 	from mt_member
//					// 	where driver=1 and driverarea like '%,` + fmt.Sprint(v["area"]) + `,%'`)
//					// if driver != nil {
//					// 	data3 := map[string]map[string]interface{}{
//					// 		"first":    {"value": `您有一个新的订单需要提货`},
//					// 		"keyword1": {"value": v["ordernum"], "color": `#36353A`},
//					// 		"keyword2": {"value": strings.Join(pname, "、"), "color": `#e31939`},
//					// 		"keyword3": {"value": nowtimes, "color": "#9c9c9c"},
//					// 		"remark":   {"value": `请您尽快前往提货`},
//					// 	}
//					// 	models.Push(fmt.Sprint(driver[0]["wx_openid_gzh"]), `cXLsiC-KrPaiYpOzNf4ZWa1ERzUt6v9uNhB-ur1IZso`, data3, models.Urlc+"/mobile/my/shop", "")
//					// }
//					data3 := map[string]map[string]interface{}{
//						"first":    {"value": `您有一个新的订单需要备货`},
//						"keyword1": {"value": v["ordernum"], "color": `#36353A`},
//						"keyword2": {"value": strings.Join(pname, "、"), "color": `#e31939`},
//						"keyword3": {"value": nowtimes, "color": "#9c9c9c"},
//						"remark":   {"value": `请您尽快处理`},
//					}
//					err := models.Push(`ozAyh0h1vSDUd65kggTBbPAN_Dlc`, `cXLsiC-KrPaiYpOzNf4ZWa1ERzUt6v9uNhB-ur1IZso`, data3, models.Urlc+"/mobile/my/shop", "")
//					beego.Debug(err)
//				}
//			}
//		}
//	}
func chaxunpng(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsExist(err) {
		return false, nil
	}
	return false, err
}

type Index struct {
	BaseController
}

func (c *Index) Get() {
	// ocdPush("3672")
	shopWhere := ""
	gbuyWhere := "a.gid = 0 and"
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
		gbuyWhere = "a.gid = " + c.ShopId + " and"
	} else {
		shopWhere = "a.gid not in (" + c.ShopIdAll + ") and"
	}
	var shoplist = make(map[string]interface{})
	var slice []interface{}
	var slice2 []interface{}
	var item = make(map[string]interface{})
	item["id"] = ""
	item["area"] = "台州市"
	if c.ShopId == "" {
		slice = append(slice, item)
	}

	shop := models.Query(`select id,city from mt_shop where status=1 and censoring=1 and style=1 order by recommend desc,sort desc,addtime desc`)
	if shop != nil {
		for i, v := range shop {
			if v["city"] != nil {
				area := models.Query(`select name from mt_area where code=` + v["city"].(string))[0]["name"]
				if area != nil {
					v["area"] = area.(string)
				}
				if v["id"].(string) == c.ShopId {
					slice = append(slice, shop[i])
					slice = append(slice, item)
				} else {
					slice2 = append(slice2, shop[i])
				}
			} else {
				shop[i] = nil
			}
		}
		slice = append(slice, slice2...)
		shoplist["currentcity"] = slice[0]
		shoplist["citylist"] = slice[1:]
	}
	var platform string
	client := c.GetString("client")
	switch client {
	case "":
		platform = "32"
	case "xapp":
		platform = "30"
	case "app":
		platform = "33"
	}
	ad := models.Query(`select a.url,b.pathname from mt_ad a left join mt_resources b on a.img=b.id where a.type=1 and a.status=1 and a.platform=` + platform + ` 
	and a.channel=59 order by a.sort desc`)
	shop_area := models.Query(`select distinct province from mt_shop where status=1 and censoring=1`)
	province := ``
	if shop_area != nil {
		for _, v := range shop_area {
			if v["province"] != nil {
				province += "," + v["province"].(string)
			}
		}
	}
	province = province[1:]
	p_area := models.Query(`select a.id,a.name,a.sid,a.style,a.brandclassify,a.program,a.structure,a.Novice,d.name as maincolumn,b.pathname,c.bid from mt_area_production a left join mt_resources b on a.img=b.id left join mt_brand_classify c on a.brandclassify=c.id left join mt_column d on a.maincolumn=d.id`)
	brandstreet := models.Query(`select id,name,bid from mt_brand_classify where status=1`)
	brandstring := ``
	if brandstreet != nil {
		for _, v := range brandstreet {
			if v["bid"] != nil {
				b_arr := strings.Split(v["bid"].(string), ",")
				if len(b_arr) >= 2 && b_arr != nil {
					brandstring += "," + b_arr[0] + "," + b_arr[1]
				} else {
					brandstring += "," + b_arr[0] + "," + "nil"
				}
			}
		}
		brandstring = brandstring[1:]
		brands := models.Query(`select a.name,a.id,b.pathname from mt_brand a left join mt_resources b on a.logo=b.id where a.id in (` + brandstring + `) order by field(a.id,` + brandstring + `)`)
		for i, v := range brandstreet {
			v["brands"] = brands[2*i : 2*(i+1)]
		}
	}
	productstruct := models.Query(`select id,name,pid from mt_goods_struct where status=1 and pid!=''`)
	if productstruct != nil {
		for _, v := range productstruct {
			column := models.Query(`select distinct a.id,a.name,b.pathname from mt_column a left join mt_resources b on a.img=b.id where a.id in (select cid from mt_product where id in (` + v["pid"].(string) + `))`)
			for i, k := range column {
				if k["pathname"] != nil {
					v["product"] = column[i]
					break
				}
			}

		}
	}

	news := models.Query(`select a.id,a.title,b.pathname from mt_news a left join mt_resources b on a.img=b.id where a.status=1 and a.censoring=1 order by a.addtime desc`)
	recommend := ProductList(`select a.id,a.price,a.title,a.mprice,b.pathname,a.isvip from mt_product a left join mt_resources b on a.img=b.id where `+shopWhere+` a.censoring=1 and a.status=1
	and a.is_show=1 order by a.addtime desc limit 6`, c.Uid)
	block := models.Query(`select a.id,a.name,a.cid,a.pid,a.intr,b.pathname from mt_index_block a left join mt_resources b on a.mobileimg=b.id where ` + gbuyWhere + ` a.status=1 order by defaultsort`)
	for i, v := range block {
		if v["pid"] != nil && v["cid"] != nil {
			v["url"] = `/mobile/index/indexblock?id=` + v["id"].(string)
		} else {
			block[i] = nil
		}
	}
	keywords := models.Query(`select title from mt_keywords where type=0 and status=1 and hot=1 order by sort desc`)
	var w []string
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.is_show=1`)
	page, _, num := models.Pagechange(c.GetString("page"), "20")
	ob := ` order by a.addtime desc`
	var lt string
	if c.GetString("page") == "" {
		lt = ` limit 6,20`
	} else {
		lt = ` limit ` + page + `,` + num
	}
	ws := strings.Join(w, ` and `)
	sql := `select a.id,a.title,a.price,a.mprice,a.isvip,b.pathname from mt_product a left join mt_resources b on a.img=b.id where ` + shopWhere + ` ` + ws + ob + lt
	product := ProductList(sql, c.Uid)
	about_tc := models.Query(`select id,name from mt_novice_column where status=1`)
	for _, v := range about_tc {
		pro := models.Query(`select id,title from mt_novice_join where status=1 and cid=?`, v["id"].(string))
		v["pro"] = pro
	}
	// column := models.Query(`select a.id,a.name,b.pathname from mt_column a left join mt_resources b on a.img=b.id where a.type='p' and a.status=1 and a.level=2 and
	// 	a.gid=0 order by a.sort desc limit 0,4`)
	var mbuy []orm.Params
	if mbuys := models.Query(`select b.pathname from mt_product a left join mt_resources b on a.img=b.id where ` + shopWhere + ` a.status=1 and a.censoring=1 
		and a.mbuy=1 order by a.sort desc,a.addtime desc limit 0,2`); mbuys != nil {
		mbuy = mbuys
	}
	var good []orm.Params
	if goods := models.Query(`select b.pathname from mt_product a left join mt_resources b on a.img=b.id where ` + shopWhere + ` a.status=1 and a.censoring=1 
		and a.good=1 order by a.sort desc,a.addtime desc limit 0,2`); goods != nil {
		good = goods
	}
	var recom orm.Params
	if recoms := models.Query(`select b.pathname from mt_product a left join mt_resources b on a.img=b.id where ` + shopWhere + ` a.status=1 and a.censoring=1 
		and a.recom=1 order by a.addtime desc limit 0,1`); recoms != nil {
		recom = recoms[0]
	}
	brand := models.Query(`select id,name from mt_brand where status=1 and censoring=1 order by recommend desc`)
	program := models.Query(`select a.id,a.activetime,a.name,a.pid,a.bgImg,b.pathname,a.intr,a.mobileImg from mt_program a left join mt_resources b on a.mobileImg=b.id where ` + gbuyWhere + ` a.status=1 order by a.sort desc`)
	// column := models.Query(`select a.id,a.name,b.pathname from mt_program a left join mt_resources b on a.img=b.id where gid=0 order by a.sort desc`)
	flag := true
	if program != nil {
		for i, v := range program {
			bgImgpathname := models.Query(`select pathname from mt_resources where id=?`, v["bgImg"])
			if bgImgpathname != nil {
				v["bgImgpathname"] = bgImgpathname[0]["pathname"]
			}
			pid := fmt.Sprint(v["pid"])
			product := models.Query(`select a.* from mt_product a where ` + shopWhere + ` a.status=1 and a.censoring=1 and a.is_show=1 and a.id in(` + pid + `)`)
			if product == nil {
				program[i] = nil
			} else {
				flag = false
			}
		}
		if flag {
			program = nil
		}
	}
	var shopid string
	if c.ShopId != "" {
		shopid = c.ShopId
	} else {
		shopid = "0"
	}
	sysRcmd := true
	if program == nil && mbuy == nil && good == nil && recom == nil {
		sysRcmd = false
	}
	beego.Debug(time.Now())
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["ad"] = ad
		js["p_area"] = p_area
		js["brandstreet"] = brandstreet
		js["productstruct"] = productstruct
		js["about_tc"] = about_tc
		js["brand"] = brand
		js["shop_area"] = shop_area
		js["mbuy"] = mbuy
		js["good"] = good
		js["recom"] = recom
		js["news"] = news
		js["sysRcmd"] = sysRcmd
		js["recommend"] = recommend
		js["product"] = product
		// js["column"] = column
		js["program"] = program
		js["block"] = block
		js["keywords"] = keywords
		js["uid"] = c.Uid
		js["shopId"] = shopid
		js["cityshop"] = shoplist
		js["currentcity"] = c.GetSession("city")
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["ad"] = ad
		c.Data["p_area"] = p_area
		c.Data["brandstreet"] = brandstreet
		c.Data["productstruct"] = productstruct
		c.Data["about_tc"] = about_tc
		c.Data["brand"] = brand
		c.Data["shop_area"] = shop_area
		c.Data["mbuy"] = mbuy
		c.Data["good"] = good
		c.Data["recom"] = recom
		c.Data["sysRcmd"] = sysRcmd
		c.Data["news"] = news
		c.Data["sysRcmd"] = sysRcmd
		c.Data["recommend"] = recommend
		c.Data["product"] = product
		//c.Data["column"] = program
		c.Data["program"] = program
		c.Data["block"] = block
		c.Data["cityshop"] = shoplist
		c.Data["keywords"] = keywords
		c.Data["uid"] = c.Uid
		c.Data["shopId"] = shopid
		c.Data["currentcity"] = c.GetSession("city")
		c.TplName = "wechat/index/index.html"
	}
}

type NewProduct struct {
	BaseController
}

func (c *NewProduct) Get() {
	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	} else {
		shopWhere = "a.gid not in (" + c.ShopIdAll + ") and"
	}
	bid := c.GetString("id")
	sql := `select a.id,a.price,a.title,a.mprice,a.minbuy,b.pathname,a.isvip,a.minbuy from mt_product a left join mt_resources b on a.img=b.id where ` + shopWhere + ` a.censoring=1 and a.status=1 
		and a.is_show=1 and a.bid=` + bid + ` order by a.addtime desc limit 10`
	newProduct := ProductList(sql, c.Uid)
	var js = make(map[string]interface{})
	js["newProduct"] = newProduct
	c.Data["json"] = js
	c.ServeJSON()
}

type IndexProduct struct {
	BaseController
}

func (c *IndexProduct) Get() {

	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	} else {
		shopWhere = "a.gid not in (" + c.ShopIdAll + ") and"
	}
	client := c.GetString("status")
	var ob string
	switch client {
	case "0":
		ob = ` order by a.addtime desc`
	case "1":
		ob = ` and recommend=1`
	case "2":
		ob = ` order by c.sellnum desc,a.sellnum desc`
	case "3":
		ob = ` and a.bid=118 order by a.addtime desc`
	}
	var lt string
	page, _, num := models.Pagechange(c.GetString("page"), "12")
	if c.GetString("page") == "" {
		lt = ` limit 0,12`
	} else {
		lt = ` limit ` + page + `,` + num
	}
	var sql string
	if client == "2" {
		sql = `select a.id,a.title,a.price,a.mprice,a.isvip,a.minbuy,b.pathname from mt_product a left join mt_resources b on a.img=b.id left join (select pid,sum( num ) as sellnum from mt_order_car where status>1 and date_sub( curdate(), interval 30 day ) <= addtime group by pid ) c on a.id=c.pid where a.status=1 and a.censoring=1 and a.is_show=1 ` + ob + lt
	} else {
		sql = `select a.id,a.title,a.price,a.mprice,a.isvip,a.minbuy,b.pathname from mt_product a left join mt_resources b on a.img=b.id where ` + shopWhere + ` a.status=1 and a.censoring=1 and a.is_show=1 ` + ob + lt
	}
	product := ProductList(sql, c.Uid)
	var js = make(map[string]interface{})
	js["product"] = product
	c.Data["json"] = js
	c.ServeJSON()
}

type IndexShopContent struct {
	BaseController
}

func (c *IndexShopContent) Get() {
	gid := c.GetString("id")
	if gid != "" {
		var shop orm.Params
		if gid == "0" {
			shop = models.Systems()
		} else {
			if shops := models.Query(`select a.*,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id where
			 a.censoring=1 and a.status=1 and a.id=` + gid); shops != nil {
				shop = shops[0]
			}
		}
		if shop == nil {
			if c.IsAjax() {
				c.ServeJSON()
			} else {
				c.TplName = "wechat/model/noshop.html"
			}
		} else {
			//阅览记录
			if err := models.Query(`update mt_shop set click=click+1 where id=` + gid); err != nil {
				beego.Debug(err)
			}
			var isfocus int
			if c.Uid != "" {
				if isc := models.Query(`select id from mt_member_focus where uid=` + c.Uid + ` and gid=` + gid); isc != nil {
					isfocus = 1
				}
				if is := models.Query(`select id from mt_systems_reading where uid=` + c.Uid + ` and gid=` + gid); is != nil {
					if err := models.Exec(`update mt_systems_reading set click=click+1,addtime='` + sword.Gettimes("") + `' where id=` + is[0]["id"].(string)); err != nil {
						beego.Debug(err)
					}
				} else {
					if err := models.Exec(`insert into mt_systems_reading (uid,gid,click,addtime) values (` + c.Uid + `,` + gid + `,0,'` + sword.Gettimes("") + `'')`); err != nil {
						beego.Debug(err)
					}
				}
			}
			if c.IsAjax() {
				var js = make(map[string]interface{})
				js["info"] = shop
				js["isfocus"] = isfocus
				c.ServeJSON()
			} else {
				c.Data["info"] = shop
				c.Data["isfocus"] = isfocus
				c.TplName = "wechat/index/store.html"
			}
		}
	} else {
		if c.IsAjax() {
			c.ServeJSON()
		} else {
			c.TplName = "wechat/model/noshop.html"
		}
	}
}

type IndexRecom struct {
	BaseController
}

func (c *IndexRecom) Get() {
	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	}
	var w []string
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.recom=1`)
	w = append(w, `a.is_show=1`)
	var nums = "10"
	if c.GetString("page") == "" {
		nums = "20"
	}
	page, _, num := models.Pagechange(c.GetString("page"), nums)
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.sort desc,a.addtime desc`
	ws := strings.Join(w, ` and `)
	sql := `select a.title,a.price,a.description,a.mprice,a.id,b.pathname,a.isvip,a.minbuy from mt_product a left join mt_resources b on a.img=b.id where ` + shopWhere + ` ` + ws + ob + lt
	infos := ProductList(sql, c.Uid)
	var info []orm.Params
	var finfo orm.Params
	var rinfo []orm.Params
	var minfo []orm.Params
	if c.GetString("page") == "" {
		length := len(infos)
		if length > 0 {
			finfo = infos[0]
			if length > 1 {
				if length <= 6 {
					rinfo = infos[1:]
				} else if length > 6 && length < 12 {
					rinfo = infos[1:7]
					minfo = infos[7:]
				} else if length >= 12 {
					rinfo = infos[1:7]
					minfo = infos[7:12]
					info = infos[12:]
				}

			}
		}
	} else {
		info = infos
	}
	ad := models.Query(`select a.url,b.pathname from mt_ad a left join mt_resources b on a.img=b.id where a.type=1 and a.status=1 and a.platform=32 
	and a.channel=60 order by a.sort desc`)
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["ad"] = ad
		js["info"] = info
		js["finfo"] = finfo
		js["rinfo"] = rinfo
		js["minfo"] = minfo
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["ad"] = ad
		c.Data["info"] = info
		c.Data["finfo"] = finfo
		c.Data["rinfo"] = rinfo
		c.Data["minfo"] = minfo
		c.TplName = "wechat/index/recom.html"
	}
}

type IndexMbuy struct {
	BaseController
}

func (c *IndexMbuy) Get() {
	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	}
	var w []string
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.mbuy=1`)
	w = append(w, `a.is_show=1`)
	page, _, num := models.Pagechange(c.GetString("page"), "")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.sort desc,a.addtime desc`
	ws := strings.Join(w, ` and `)
	sql := `select a.title,a.price,a.isvip,a.description,a.mprice,a.id,a.inventory,b.pathname,a.minbuy from mt_product a left join mt_resources b on a.img=b.id where ` + shopWhere + ` ` + ws + ob + lt
	infos := ProductList(sql, c.Uid)
	var info []orm.Params
	var finfo orm.Params
	if c.GetString("page") == "" {
		length := len(infos)
		if length > 0 {
			finfo = infos[0]
			info = infos[1:]
		}
	} else {
		info = infos
	}
	ad := models.Query(`select a.url,b.pathname from mt_ad a left join mt_resources b on a.img=b.id where a.type=1 and a.status=1 and a.platform=32 
	and a.channel=62 order by a.sort desc`)
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["ad"] = ad
		js["info"] = info
		js["finfo"] = finfo
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["ad"] = ad
		c.Data["info"] = info
		c.Data["finfo"] = finfo
		c.TplName = "wechat/index/mbuy.html"
	}
}

type IndexGood struct {
	BaseController
}

func (c *IndexGood) Get() {
	shopWhere := ""
	// if c.ShopId != "" {
	// 	shopWhere = "a.gid = " + c.ShopId + " and"
	// }
	var w []string
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.good=1`)
	w = append(w, `a.is_show=1`)
	page, _, num := models.Pagechange(c.GetString("page"), "10")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.sort desc,a.addtime desc`
	ws := strings.Join(w, ` and `)
	sql := `select a.title,a.keywords,a.isvip,a.price,a.gid,a.id,a.sellnum,a.minbuy,a.issellnum,b.pathname,c.name as gname,d.name as shen from mt_product a left join mt_resources b on a.img=b.id left join mt_shop c on a.gid=c.id 
	left join mt_area d on c.province=d.code where ` + shopWhere + ` ` + ws + ob + lt
	infos := ProductList(sql, c.Uid)
	var info []orm.Params
	var rinfo []orm.Params
	if c.GetString("page") == "" {
		length := len(infos)
		if length > 6 {
			rinfo = infos[0:6]
			info = infos[6:]
		} else {
			rinfo = infos
		}
	} else {
		info = infos
	}
	ad := models.Query(`select a.url,b.pathname from mt_ad a left join mt_resources b on a.img=b.id where a.type=1 and a.status=1 and a.platform=32 
	and a.channel=61 order by a.sort desc`)
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["ad"] = ad
		js["info"] = info
		js["rinfo"] = rinfo
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["ad"] = ad
		c.Data["info"] = info
		c.Data["rinfo"] = rinfo
		c.TplName = "wechat/index/good.html"
	}
}

type IndexBrand struct {
	BaseController
}

func (c *IndexBrand) Get() {
	brand := models.Query(`select a.id,b.pathname from mt_brand a left join mt_resources b on a.logo=b.id where a.censoring=1 
		and a.status=1 and a.is_show=1 order by a.recommend desc,a.sort desc`)
	var w []string
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.style=1`)
	page, _, num := models.Pagechange(c.GetString("page"), "10")
	lt := ` limit ` + page + `,` + num
	var ob string
	ob = ` order by a.recommend desc,a.sort desc`
	coor := c.GetSession("coor")
	ws := strings.Join(w, ` and `)
	var sql = `select a.id,a.name,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id where ` + ws + ob + lt
	if coor != nil {
		coors := coor.(string)
		coorss := strings.Split(coors, ",")
		ob = ` order by jl`
		sql = `select a.id,a.name,b.pathname,st_distance(point(` + coorss[1] + `,` + coorss[0] + `),
		point(a.longitude,a.latitude)) as jl from mt_shop a left join mt_resources b on a.logo=b.id where ` + ws + ob + lt
	}
	info := models.Query(sql)
	for _, v := range info {
		product := ProductList(`select a.id,b.pathname,a.price,a.isvip from mt_product a left join mt_resources b on a.img=b.id where 
				a.censoring=1 and a.status=1 and a.is_show=1 and a.gid=`+v["id"].(string)+` order by a.grecommend desc,a.gsort desc limit 3`, c.Uid)
		v["product"] = product
	}
	product := ProductList(`select a.id,b.pathname,a.price,a.isvip from mt_product a left join mt_resources b on a.img=b.id left 
	join mt_shop c on a.gid=c.id where a.censoring=1 and a.status=1 and a.is_show=1 and (a.gid=0 or c.style=0) order by a.grecommend desc,a.gsort desc limit 3`, c.Uid)
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["info"] = info
		js["product"] = product
		js["brand"] = brand
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.Data["product"] = product
		c.Data["brand"] = brand
		c.TplName = "wechat/index/brand.html"
	}
}

type IndexBrandStreet struct {
	BaseController
}

func (c *IndexBrandStreet) Get() {
	page, _, num := models.Pagechange(c.GetString("page"), "20")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.recommend desc,a.sort desc`
	id := c.GetString("id")
	w := ""
	bid := c.GetString("bid")
	if bid != "" {
		w += ` and a.id in(` + bid + `)`
	}
	search := c.GetString("search")
	if search != "" {
		w += ` and a.name like '%` + search + `%'`
	} else {
		w += ` and a.is_show=1`
	}
	info := make(map[string]interface{})
	classifyId := ""
	classify := models.Query(`select a.id,a.name,a.bid,b.pathname from mt_brand_classify a 
		left join mt_resources b on a.img=b.id 
		where a.status=1`)
	if id != "" {
		classifyId = id
		img := models.Query(`select b.pathname from mt_brand_classify a left join mt_resources b on a.img=b.id where a.id=?`, classifyId)[0]
		if img["pathname"] != nil {
			info["pathname"] = img["pathname"].(string)
		}
	} else {
		var imgs []string
		for _, v := range classify {
			if v["pathname"] != nil {
				imgs = append(imgs, v["pathname"].(string))
			}
			if len(imgs) != 0 {
				info["images"] = imgs
			}
		}
	}
	brand := models.Query(`select a.id,a.cid,a.name,b.pathname from mt_brand a left join mt_resources b on 
		a.logo=b.id where a.status=1 and a.censoring=1` + w + ob + lt)
	for _, v := range brand {
		var keyword []string
		if v["cid"] != nil {
			cid := strings.Trim(v["cid"].(string), ",")
			column := models.Query(`select name from mt_column where id in(` + cid + `)`)
			for _, v := range column {
				keyword = append(keyword, v["name"].(string))
			}
		}
		v["keyword"] = keyword
		products := models.Query(`select a.id,b.pathname from mt_product a left join mt_resources b on a.img=b.id 
			where a.status=1 and a.censoring=1 and a.gid not in(`+c.ShopIdAll+`) and a.bid=? order by a.addtime desc limit 0, 4`, v["id"])
		if products != nil {
			v["products"] = products
		}
	}
	if c.IsAjax() {
		json := make(map[string]interface{})
		json["brand"] = brand
		json["classify"] = classify
		json["classifyId"] = classifyId
		json["info"] = info
		c.Data["json"] = json
		c.ServeJSON()
	}
	c.Data["brand"] = brand
	c.Data["classifyId"] = classifyId
	c.Data["classify"] = classify
	c.Data["info"] = info
	c.TplName = "wechat/index/brand-street.html"
}

type IndexShop struct {
	BaseController
}

func (c *IndexShop) Get() {
	id := c.GetString("id")
	beego.Debug(id)
	if id == "" {
		return
	}
	var shop orm.Params
	if id == "0" {
		shop = models.Systems()
	} else {
		if shops := models.Query(`select a.id,a.name,a.bid,b.pathname,c.pathname as gzhpath,d.pathname as apppath,e.pathname as webpath
		,f.pathname as xapppath,g.type,g.relevance from mt_shop a left join mt_resources b on a.logo=b.id left join mt_resources c on a.gzhimg=c.id 
		left join mt_resources d on a.appimg=d.id left join mt_resources e on a.webimg=e.id left join mt_resources f on a.xappimg=f.id left join mt_brand g on g.id=a.bid 
		where a.status=1 and a.censoring=1 and a.id=` + id); shops != nil {
			shop = shops[0]
		} else {
			beego.Debug(1)
			return
		}
	}
	shop["fnum"] = models.Query(`select count(a.id) as num from mt_member_focus a where a.gid=` + id)[0]["num"]
	shop["isfocus"] = 0
	if c.Uid != "" {
		if isfocus := models.Query(`select id from mt_member_focus where uid=` + c.Uid + ` and gid=` + id); isfocus != nil {
			shop["isfocus"] = 1
		}
	}
	var w []string
	if shop["bid"] != nil {
		if shop["type"] != nil {
			if shop["type"].(string) == "1" {
				w = append(w, `a.bid in(`+shop["relevance"].(string)[:len(shop["relevance"].(string))-1]+`)`)
			} else {
				w = append(w, `a.bid=`+shop["bid"].(string))
			}
		} else {
			w = append(w, `a.gid=`+shop["id"].(string))
		}
	}
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.is_show=1`)
	now := time.Now().Local()
	now.AddDate(0, -1, 0)
	nowtime := now.Format(Tparse)

	ws := strings.Join(w, ` and `)
	all := models.Query(`select count(a.id) as num from mt_product a left join mt_brand b on a.bid=b.id where ` + ws + ` group by a.title`)
	news := models.Query(`select count(a.id) as num from mt_product a left join mt_brand b on a.bid=b.id where ` + ws + ` and a.addtime>'` + nowtime + `' group by a.title`)
	hot := models.Query(`select count(a.id) as num from mt_product a left join mt_brand b on a.bid=b.id where ` + ws + ` and a.recommend=1 group by a.title`)
	// column := models.Query(`select distinct a.cid,b.name,b.level from (select a.cid from mt_product a where ` + ws + `) a left join mt_column b on a.cid=b.id where b.type='p' and b.status=1 group by b.name order by b.level`)
	service := models.Query(`select id,monicker from mt_member where status=1 and service=1`)
	beego.Debug("wechat/index/shop.html")
	if c.IsAjax() {
		beego.Debug("1")
		var js = make(map[string]interface{})
		js["shop"] = shop
		js["gid"] = service
		js["all"] = len(all)
		js["new"] = len(news)
		js["hot"] = len(hot)
		// js["column"] = column
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		beego.Debug("2")
		c.Data["gid"] = service
		c.Data["s_title"] = shop["name"]
		c.Data["s_url"] = Url + c.Ctx.Request.RequestURI
		c.Data["shop"] = shop
		c.Data["all"] = len(all)
		c.Data["news"] = len(news)
		c.Data["hot"] = len(hot)
		// c.Data["column"] = column

		c.TplName = "wechat/index/shop.html"
	}
}
func (c *IndexShop) Post() {
	defer c.ServeJSON()
	if c.Uid == "" {
		c.Data["json"] = 2
	} else {
		id := c.GetString("id")
		if id != "" {
			if is := models.Query(`select id from mt_member_focus where uid=` + c.Uid + ` and gid=` + id); is != nil {
				if err := models.Exec(`delete from mt_member_focus where id=` + is[0]["id"].(string)); err != nil {
					beego.Debug(err)
					c.Data["json"] = 0
				} else {
					c.Data["json"] = 1
				}
			} else {
				if err := models.Exec(`insert into mt_member_focus (uid,gid,addtime) values (` + c.Uid + `,` + id + `,'` + sword.Gettimes("") + `')`); err != nil {
					beego.Debug(err)
					c.Data["json"] = 0
				} else {
					c.Data["json"] = 1
				}
			}
		}
	}
}

type IndexShopProduct struct {
	BaseController
}

func (c *IndexShopProduct) Get() {
	shop := models.Query(`select b.type,b.relevance from mt_shop a left join mt_brand b on a.bid=b.id where a.id=` + c.GetString("id"))[0]
	var w []string
	if c.GetString("bid") != "0" {
		if shop["type"] != nil {
			if shop["type"].(string) == "1" {
				w = append(w, `a.bid in(`+shop["relevance"].(string)[:len(shop["relevance"].(string))-1]+`)`)
			} else {
				w = append(w, `a.bid=`+c.GetString("bid"))
			}
		}
	}
	if c.ShopId != "" {
		w = append(w, `a.gid=`+c.ShopId)
	} else {
		w = append(w, `a.gid not in(select id from mt_shop where style=1)`)
	}
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.is_show=1`)
	page, _, num := models.Pagechange(c.GetString("page"), "10")
	lt := ` limit ` + page + `,` + num
	status := c.GetString("status")
	sort := c.GetString("sort")
	now := time.Now().Local()
	now.AddDate(0, -1, 0)
	nowtime := now.Format(Tparse)
	var ob string
	switch status {
	case "0", "":
		if sort == "0" {
			ob = ` order by a.price,a.recommend desc,a.gsort desc`
		} else if sort == "1" {
			ob = ` order by a.price desc,a.recommend desc,a.gsort desc`
		} else if sort == "2" {
			ob = ` order by a.sellnum desc,a.recommend desc,a.gsort desc`
		} else if sort == "3" {
			ob = ` order by a.addtime desc,a.recommend desc,a.gsort desc`
		} else {
			ob = ` order by a.recommend desc,a.gsort desc,a.id desc `
		}
	case "1":
		w = append(w, `a.addtime>'`+nowtime+`'`)
		ob = ` order by a.addtime desc`
	case "2":
		w = append(w, `a.recommend=1`)
		ob = ` order by a.sort desc`
	}

	ws := strings.Join(w, ` and `)
	sql := `select  a.id,a.title,a.price,a.isvip,a.addtime,a.mprice,a.minbuy,b.pathname,a.keywords,a.issellnum,a.sellnum,a.addtime
	 from mt_product a left join mt_resources b on a.img=b.id left join mt_shop c on a.gid=c.id where ` + ws + ` group by a.title` + ob + lt
	beego.Debug(sql)
	info := ProductList(sql, c.Uid)
	var js = make(map[string]interface{})
	js["info"] = info
	c.Data["status"] = status
	c.Data["json"] = js
	c.ServeJSON()
}

type IndexShopList struct {
	BaseController
}

func (c *IndexShopList) Get() {
	sid := c.GetString("sid")
	page, _, num := models.Pagechange(c.GetString("page"), "15")
	lt := ` limit ` + page + `,` + num
	//beego.Debug(`select a.id,a.name,a.click,a.score,a.type,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id
	//	where a.status=1 and a.id in(` + sid + `) and a.censoring=1 order by a.style desc` + lt)
	shoplist := models.Query(`select a.id,a.name,a.click,a.score,a.type,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id 
		where a.status=1 and a.id in(` + sid + `) and a.censoring=1 order by a.style desc` + lt)
	for _, v := range shoplist {
		if v["type"] != nil && v["type"] != "" {
			columnid := v["type"].(string)[1 : len(v["type"].(string))-1]
			if columnid != `` {
				typeTag := models.Query(`select name from mt_column where id in(` + columnid + `)`)
				var name []string
				for _, a := range typeTag {
					name = append(name, a["name"].(string))
				}
				v["type"] = strings.Join(name, `、`)
			}
		} else {
			v["type"] = ""
		}
	}

	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["info"] = shoplist
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["info"] = shoplist
		c.TplName = "wechat/index/shopList.html"
	}
}

type IndexShopSearch struct {
	BaseController
}

func (c *IndexShopSearch) Get() {
	id := c.GetString("id")
	keywords := c.GetString("keywords")
	keywords2 := c.GetString("keywords2")
	if keywords2 != "" {
		keywords = keywords2
	}
	cid := c.GetString("cid")
	if id == "" {
		return
	}
	var w []string
	if id == "0" {
		w = append(w, `(a.bid=`+id+` or c.style=0)`)
	} else {
		w = append(w, `a.bid=`+id)
	}
	if c.ShopId != "" {
		w = append(w, `a.gid=`+c.ShopId)
	} else {
		w = append(w, `a.gid not in(select id from mt_shop where style=1)`)
	}
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	if cid != "" {
		w = append(w, `a.gcid=`+cid)
		cname := models.Query(`select name from mt_column where id=` + cid)[0]["name"]
		c.Data["s_title"] = cname
		c.Data["title"] = cname
	}
	var w2 []string
	if keywords != "" {
		mtkeywords := models.Query(`select id from mt_keywords where title like '%` + keywords + `%' and status=1 and type=1`)
		w2 = append(w2, `a.title like '%`+keywords+`%'`)
		w2 = append(w2, `a.keywords like '%`+keywords+`%'`)
		w2 = append(w2, `a.model like '%`+keywords+`%'`)
		for _, v := range mtkeywords {
			w2 = append(w2, `a.title like '%`+v["id"].(string)+`%'`)
			w2 = append(w2, `a.keywords like '%`+v["id"].(string)+`%'`)
			w2 = append(w2, `a.model like '%`+v["id"].(string)+`%'`)
		}
		c.Data["s_title"] = `搜索结果`
		c.Data["s_desc"] = `关键词:` + keywords
		c.Data["title"] = `搜索结果`
	}
	w2s := strings.Join(w2, ` or `)
	if w2s != "" {
		w2s = `(` + w2s + `)`
		w = append(w, w2s)
	}
	page, _, num := models.Pagechange(c.GetString("page"), "20")
	lt := ` limit ` + page + `,` + num
	status := c.GetString("status")
	statusajax := c.GetString("statusajax")
	var ob string
	if statusajax == "" {
		switch status {
		case "":
			ob = ` order by a.grecommend desc,a.gsort desc`
		case "0":
			ob = ` order by a.price desc`
		case "1": //价格
			ob = ` order by a.price`
		case "2": //销量
			ob = ` order by a.sellnum desc`
		case "3": //销量
			ob = ` order by a.addtime desc`
			c.Data["s_title"] = "新品"
		}
	} else {
		switch statusajax {
		case "":
			ob = ` order by a.grecommend desc,a.gsort desc`
		case "0":
			ob = ` order by a.price desc`
		case "1": //价格
			ob = ` order by a.price`
		case "2": //销量
			ob = ` order by a.sellnum desc`
		case "3": //销量
			ob = ` order by a.addtime desc`
		}
	}
	ws := strings.Join(w, ` and `)
	// beego.Debug(ws)
	sql := `select a.title,a.keywords,a.isvip,a.price,a.gid,a.id,a.sellnum,a.minbuy,a.issellnum,b.pathname,a.addtime from mt_product a left join
	 mt_resources b on a.img=b.id left join mt_shop c on a.gid=c.id where ` + ws + ob + lt
	info := ProductList(sql, c.Uid)
	if c.IsAjax() {
		c.Data["json"] = info
		c.ServeJSON()
	} else {
		c.Data["s_url"] = Url + c.Ctx.Request.RequestURI
		c.Data["id"] = id
		c.Data["info"] = info
		c.TplName = "wechat/index/search_result.html"
	}
}

type ProductBarcode struct {
	BaseController
}

func (c *ProductBarcode) Get() {
	defer c.ServeJSON()
	barcode := c.GetString("barcode")
	beego.Debug(barcode)
	if barcode == "" {
		c.Data["json"] = 0
		return
	}
	gid := c.ShopId
	var sql string
	if gid == "" {
		sql = `
		select a.id,b.style 
		from mt_product a 
		left join mt_shop b on a.gid=b.id 
		where a.barcode='` + barcode + `' and a.censoring=1 and a.status=1   
		order by b.style`
	} else if gid == "0" {
		sql = `
		select a.id
		from mt_product a 
		join (select * from mt_shop where style=0) b on a.gid=b.id 
		where a.barcode='` + barcode + `' and a.censoring=1 and a.status=1  `
	} else {
		sql = `
		select a.id
		from mt_product a 
		where a.barcode='` + barcode + `' and a.censoring=1 and a.status=1 and a.gid=` + gid
	}
	if is := models.Query(sql); is != nil {
		c.Data["json"] = is[0]["id"]
	} else {
		c.Data["json"] = 0
	}
}

type IndexNewsContent struct {
	BaseController
}

func (c *IndexNewsContent) Get() {
	id := c.GetString("id")
	news := models.Query(`select title,click,keywords,intr,content,date_format(addtime,'%Y-%m-%d') as addtime from mt_news where status=1 and censoring=1 and id=` + id)
	for _, v := range news {
		v["content"] = sword.UeditorChange(models.Urlc, fmt.Sprint(v["content"]))
	}
	if news == nil {
		return
	}
	if err := models.Query(`update mt_news set click=click+1 where id=` + id); err != nil {
		beego.Debug(err)
	}
	if c.IsAjax() {
		c.Data["json"] = news[0]
		c.ServeJSON()
	} else {
		c.Data["info"] = news[0]
		c.TplName = "wechat/index/news_content.html"
	}
}

type IndexNewsList struct {
	BaseController
}

func (c *IndexNewsList) Get() {
	var lt string
	if c.GetString("page") == "" {
		lt = ` limit 0, 10`
	} else {
		page, _, num := models.Pagechange(c.GetString("page"), "10")
		lt = ` limit ` + page + `, ` + num
	}
	news := models.Query(`select a.id,a.title,a.addtime,a.intr,a.click,a.content from mt_news a where status=1 and censoring=1 order by recommend desc,sort desc,addtime desc` + lt)
	for _, v := range news {
		v["pathname"] = sword.UeditorImg(v["content"].(string))
	}
	if c.IsAjax() {
		if len(news) == 0 {
			c.Data["json"] = 0
		} else {
			c.Data["json"] = news
		}
		c.ServeJSON()
	} else {
		c.Data["news"] = news
		c.TplName = "wechat/index/news_list.html"
	}
}

type IndexShopSales struct {
	BaseController
}

func (c *IndexShopSales) Get() {
	// utils.Display("i", c.Input())
	gid := c.GetString("id")
	nows := sword.Gettimes("")
	info := models.Query(`
		select a.title,date_format(a.starttime,'%Y-%m-%d') as starttime,date_format(a.endtime,'%Y-%m-%d') as endtime,a.pid,b.price,a.price as vprice,c.pathname  
		from mt_active_sales a 
		left join mt_product b on a.pid=b.id 
		left join mt_resources c on b.img=c.id 
		where a.status=1 and a.end=0 and a.starttime<=? and a.endtime>? and a.gid=?`, nows, nows, gid)
	beego.Debug(info)
	for _, v := range info {
		v["active"] = 2
	}
	if c.IsAjax() {
		c.Data["json"] = info
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.TplName = "wechat/index/shop_sales.html"
	}
}

type IndexBlock struct {
	BaseController
}

func (c *IndexBlock) Get() {
	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	}
	id := c.GetString("id")
	info := models.Query(`select * from mt_index_block where id=` + id)[0]
	pid := fmt.Sprint(info["pid"])
	product := models.Query(`
		select a.id,a.title,b.pathname from mt_product a 
		left join mt_resources b on a.img=b.id 
		where ` + shopWhere + ` a.censoring=1 and a.status=1 and a.id in(` + pid + `)
		order by a.cid`)
	for _, p := range product {
		price, _, _ := ProductPrice(p["id"].(string), c.Uid)
		p["price"] = price
	}
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["product"] = product
		c.Data["json"] = js
		beego.Debug(js)
		c.ServeJSON()
	} else {
		c.Data["product"] = product
		c.Data["s_title"] = info["name"]
		c.TplName = "wechat/index/block.html"
	}
}

type IndexProgram struct {
	BaseController
}

func (c *IndexProgram) Get() {
	beego.Debug(c.Uid)
	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	}
	id := c.GetString("id")
	info := models.Query(`select a.*,b.pathname from mt_program a left join mt_resources b on a.img=b.id where a.id=?`, id)[0]
	// cid := fmt.Sprint(info["cid"])
	pid := fmt.Sprint(info["pid"])
	product := models.Query(`
			select a.id,a.title,a.number,a.price,a.number,a.minbuy,b.pathname  
			from mt_product a 
			left join mt_resources b on a.img=b.id 
			where ` + shopWhere + ` a.censoring=1 and a.status=1 and a.id in(` + pid + `) 
			order by a.sort desc`)
	for _, p := range product {
		price, _, _ := ProductPrice(p["id"].(string), c.Uid)
		p["price"] = price
		// p["priceone"] = price
		// number := fmt.Sprint(p["number"])
		// price = sword.Mult(price, number)
	}

	// column := models.Query(`select id,name from mt_column where id in(` + cid + `)`)
	// for _, v := range column {
	// 	var cids []string
	// 	columnson := models.Query(`select id from mt_column where status=1 and pid=?`, v["id"])
	// 	for _, s := range columnson {
	// 		cids = append(cids, fmt.Sprint(s["id"]))
	// 		columnson2 := models.Query(`select id from mt_column where status=1 and pid=?`, s["id"])
	// 		for _, s2 := range columnson2 {
	// 			cids = append(cids, fmt.Sprint(s2["id"]))
	// 		}
	// 	}
	// 	cidss := strings.Join(cids, ",")

	// }
	if c.IsAjax() {
		defer c.ServeJSON()
		var js = make(map[string]interface{})
		c.Data["json"] = &js
		js["product"] = product
		js["info"] = info
	} else {
		c.Data["product"] = product
		c.Data["s_title"] = info["name"]
		c.Data["s_desc"] = info["intr"]
		c.Data["s_url"] = Url + c.Ctx.Request.RequestURI
		c.Data["s_img"] = info["pathname"]
		c.TplName = "wechat/index/program.html"
	}
}
func (c *IndexProgram) Post() {
	defer c.ServeJSON()
	if c.Uid == "" {
		c.Data["json"] = 962
		return
	}
	c.Data["json"] = 1
	id := c.GetString("id")
	num := c.GetString("num")
	ids := strings.Split(id, ",")
	nums := strings.Split(num, ",")
	nows := sword.Gettimes("")
	for k, v := range ids {
		price, gid, tid := ProductPrice(v, c.Uid)
		if ishave := models.Query(`
			select id 
			from mt_order_car 
			where status=1 and uid=? and pid=?`, c.Uid, v); ishave != nil {
			if err := models.Exec(`
				update mt_order_car 
				set price=?,num=num+? 
				where id=?`, price, nums[k], ishave[0]["id"]); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
				return
			}
		} else {
			if err := models.Exec(`
				insert into mt_order_car (gid,uid,pid,num,price,status,addtime,type) 
				values (?,?,?,?,?,1,?,?)`, gid, c.Uid, v, nums[k], price, nows, tid); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
				return
			}
		}
	}
}

type AppDownload struct {
	beego.Controller
}

func (s *AppDownload) Get() {
	s.TplName = "wechat/index/download.html"
}

type IndexArea struct {
	BaseController
}

func (c *IndexArea) Get() {
	w := ` a.style=1 and a.censoring=1 and a.status=1`
	shop := models.Query(`select a.city from mt_shop a where` + w)
	var shopCode []string
	for _, v := range shop {
		shopCode = append(shopCode, v["city"].(string))
	}
	var city []string
	cities := models.Query(`select code,name from mt_area where pid in (select id from mt_area where pid=0)`)
	for _, v := range cities {
		name := v["name"].(string)
		for _, code := range shopCode {
			if code == v["code"].(string) {
				name += "1"
			}
		}
		city = append(city, name)
	}
	hotCity := models.Query(`select b.name from mt_shop a left join mt_area b on a.city=b.code where` + w + ` and a.hot=1`)
	if c.IsAjax() {
		js := make(map[string]interface{})
		js["citylist"] = city
		js["hotCity"] = hotCity
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["citylist"] = city
		c.Data["hotCity"] = hotCity
		c.TplName = "wechat/index/area-select.html"
	}
}

type IndexNoviceJoin struct {
	BaseController
}

func (c *IndexNoviceJoin) Get() {
	page, _, num := models.Pagechange(c.GetString("page"), "")
	system := models.Systems()
	search := c.GetString("search")
	cid := c.GetString("cid")
	w := ` a.status=1 and a.censoring=1`
	lt := ` limit ` + page + `, ` + num
	o := ` order by a.sort desc, a.modifytime desc`
	if search != "" {
		w += ` and (a.title like '%` + search + `%' or a.intro like '%` + search + `%')`
	}
	if cid != "" {
		w += ` and a.cid=` + cid
	}
	column := models.Query(`select a.id,a.name,b.pathname from mt_novice_column a 
		left join mt_resources b on a.img=b.id 
		where a.status=1`)
	ctn := models.Query(`select a.id,a.title,a.intro,a.modifytime from mt_novice_join a where` + w + o + lt)
	bgimg := [4]string{"/static/img/bb1.jpg", "/static/img/bb2.jpg", "/static/img/bb3.jpg", "/static/img/bb4.jpg"}
	if c.IsAjax() {
		js := make(map[string]interface{})
		js["column"] = column
		js["ctn"] = ctn
		js["bgimg"] = bgimg
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["column"] = column
		c.Data["cid"] = cid
		c.Data["ctn"] = ctn
		c.Data["bgimg"] = bgimg
		c.Data["system"] = system
		c.TplName = "wechat/index/novicejoin.html"
	}
}

type IndexNoviceCtn struct {
	BaseController
}

func (c *IndexNoviceCtn) Get() {
	id := c.GetString("id")
	ctn := models.Query(`select title,content,click,modifytime from mt_novice_join where id=?`, id)
	if c.GetString("parse") == "1" {
		for _, v := range ctn {
			v["content"] = sword.UeditorChange(models.Urlc, fmt.Sprint(v["content"]))
		}
	}
	err := models.Exec(`update mt_novice_join set click=click+1 where id=?`, id)
	if err != nil {
		beego.Debug(err)
	}
	if c.IsAjax() {
		js := make(map[string]interface{})
		js["ctn"] = ctn[0]
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["info"] = ctn[0]
		c.TplName = "wechat/index/novicectn.html"
	}
}

type PrivacyAgreement struct {
	BaseController
}

func (c *PrivacyAgreement) Get() {
	pa := models.Query(`select privacyAgreement from mt_systems`)[0]["privacyAgreement"]
	if c.IsAjax() {
		js := make(map[string]interface{})
		js["pa"] = pa
		c.Data["json"] = js
		c.ServeJSON()
	}
}
